//
//  DisplayView.m
//  TextToImg
//
//  Created by Garenge on 2019/M/29.
//  Copyright © 2019 鹏鹏. All rights reserved.
//

#import "DisplayView.h"
#import "AppTool.h"

@interface DisplayView()

@end

@implementation DisplayView

- (CGFloat)topMargin {
    if (_topMargin > 0.0) {
        return _topMargin;
    } else {
        _topMargin = 40.0;
        return _topMargin;
    }
}

- (CGFloat)sideMargin {
    if (_sideMargin > 0.0) {
        return _sideMargin;
    } else {
        _sideMargin = 20.0;
        return _sideMargin;
    }
}

- (NSString *)contentString {
    if (nil == _contentString) {
        _contentString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test.text" ofType:nil] encoding:NSUTF8StringEncoding error:nil];
    }
    return _contentString;
}

- (NSMutableDictionary *)mDic {
    if (nil == _mDic) {
        // set the font type and size
        _mDic = [NSMutableDictionary dictionary];
        
        // 颜色
        //        [mDic setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
        // 字体
        [_mDic setObject:APPUsualFontWithSize(15) forKey:NSFontAttributeName];
        // 段间距
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
        [paragraphStyle setParagraphSpacing:20];
        [_mDic setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    }
    return _mDic;
}

- (void)reloadDisplayWithContent:(NSString *)content attribute:(NSMutableDictionary *)mDic topMargin:(CGFloat)topMargin sideMargin:(CGFloat)sideMargin {
    self.topMargin = topMargin;
    self.sideMargin = sideMargin;
    self.contentString = content;
    self.mDic = mDic;
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGRect newRect = CGRectMake(self.sideMargin, self.topMargin, rect.size.width - 2 * self.sideMargin, rect.size.height - 2 * self.topMargin);
    [self.contentString drawInRect:newRect withAttributes:self.mDic];
}

@end
